<template>
  <scroll
    class="index-list"
    :probe-type = 3
    ref="scrollRef"
    @scroll="onScroll"
  >
    <ul ref="groupRef">
      <li
       v-for="group in data"
       :key="group.title"
       class="group"
      >
        <h2 class="title">{{group.title}}</h2>
        <ul>
          <li
          v-for="item in group.list"
          :key="item.id"
          class="item"
          @click="onItemClick(item)"
          >
            <img class="avatar" v-lazy="item.pic">
            <span class="name">{{item.name}}</span>

          </li>
        </ul>

      </li>

    </ul>

    <div
      class="fixed"
      v-show="fixedTitle"
      :style="fixedStyle"
    >
      <div class="fixed-title">{{fixedTitle}}</div>
    </div>
<!--  右边的内容  -->
    <div class="shortcut"
    @touchstart.stop.prevent="onShortcutTouchStart"
    @touchmove.stop.prevent="onShortcutTouchMove"
    @touchend.stop.prevent
    >
      <ul>
        <li
          class="item"
          v-for="(item,index) in shortcutList"
          :key="item"
          :data-index="index"
          :class="{'current':currentIndex ===index }"
        >
        {{item}}
        </li>
      </ul>

    </div>


  </scroll>


</template>

<script>
import Scroll from '@/components/wrap-scroll/index'
import useFixed from './use-fixed'
import useShortcut from './use-shortcut'

export default {
  name: "index-list",
  props:{
    data:{
      type: Array,
      default() {
        return []
      }
    }
  },
  components:{
    Scroll
  },
  emits:['select'],
  setup(props,{emit}){
    const { groupRef,onScroll ,currentIndex , fixedTitle, fixedStyle} = useFixed(props)
    console.log('fixedTitle=' + fixedTitle == true)
    const  {shortcutList, onShortcutTouchStart, onShortcutTouchMove,scrollRef,} = useShortcut(props,groupRef)



    function onItemClick(item) {
      emit('select', item)
    }
    return{
      onItemClick,
      groupRef,
      shortcutList,
      onScroll,
      currentIndex,
      fixedTitle,
      fixedStyle,
      onShortcutTouchStart,
      onShortcutTouchMove,
      scrollRef
    }

  }
}
</script>

<style lang="scss" scoped>
  .index-list{
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: $color-background;
    .group{
      padding-bottom: 30px;
      .title{
        height: 30px;
        line-height: 30px;
        padding-left: 20px;
        font-size: $font-size-small;
        color: $color-text-l;
        background: $color-highlight-background;
      }
      .item{
        display: flex;
        padding: 20px 0 0 30px;
        align-items: center;
        .avatar {
          width: 50px;
          height: 50px;
          border-radius: 50%;
        }
        .name {
          margin-left: 20px;
          color: $color-text-l;
          font-size: $font-size-medium;
        }
      }
    }
    .fixed{
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      .fixed-title{
        height: 30px;
        line-height: 30px;
        padding-left: 20px;
        font-size: $font-size-small;
        color: $color-text-l;
        background: $color-highlight-background;
      }
    }
    .shortcut {
      position: absolute;
      right: 4px;
      top: 50%;
      transform: translateY(-50%);
      width: 20px;
      border-radius: 10px;
      background: $color-background-d;
      font-family: Helvetica;
      padding: 20px 0;
      text-align: center;
      .item {
        padding: 3px;
        line-height: 1;
        color: $color-text-l;
        font-size: $font-size-small;
        &.current {
          color: $color-theme
        }
      }
    }
  }

</style>
